<script lang="ts" setup>
import { patientListApi, removePatientApi } from '@/apis/patient'
import { useConsultStore } from '@/store/consult'
import type { PatientItem } from '@/types/patient'
import { onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'

const store = useConsultStore()

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])

// 0. 选择患者
const props = defineProps<{
  type: 'consult' | '' 
}>()

onShow(()=>{
  if(props.type === 'consult'){
    // 0.1 修改标题
    uni.setNavigationBarTitle({title: '选择患者'})
  }
})

const selectPatient = (id: string)=>{
 if(props.type !== 'consult')  return
 store.patientId = id
}

const nextStep = ()=>{
  if(store.patientId === '') return uni.showToast({title: '请选择患者', icon: 'none'})
  // 跳转到下一个页面
  uni.navigateTo({ url: '/subpkg_consult/payment/index' })
}



// 1. 获取患者列表数据
const patinetList = ref<PatientItem[]>([])
// 家庭档案（患者）列表
async function getPatientList() {
  // 患者列表接口
  const data = await patientListApi()
  // 渲染接口数据
  patinetList.value = data
  // 1.1 设置选中的患者
  if(props.type === 'consult' && patinetList.value.length > 0){
    // 如果没有默认患者, 则用数组中第一个患者
    store.patientId = patinetList.value[0].id
    // 如果有默认患者, 则用默认患者
    const item =  patinetList.value.find(item => item.defaultFlag === 1)
    if(item?.id){
      store.patientId = item.id
    }
  } else{
    store.patientId = ''
  }
}
onShow(() => {
  getPatientList()
})

// 2. 滑动操作点击
const onSwipeActionClick = async (id: string, index: number) => {
  console.log('3232');
  
  // 调用删除患者接口
  await removePatientApi(id)
  // Vue 实例中的数据也要同步删除
  patinetList.value.splice(index, 1)
}
</script>

<template>
  <scroll-page>
    <view class="archive-page">

      <view v-if="type === 'consult'">
        <view>请选择患者信息</view>
        <view class="archive-tips">以便医生给出更准确的治疗, 信息仅医生可见</view>
      </view>

      <uni-swipe-action>
        <uni-swipe-action-item :right-options="swipeOptions" v-for="(item, index) in patinetList" :key="item.id"
          @click="onSwipeActionClick(item.id, index)">
          <view class="archive-card" :class="{ active: (store.patientId === '' && item.defaultFlag === 1) || store.patientId === item.id  }" @click="selectPatient(item.id)">
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{ item.idCard }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator hover-class="none" class="edit-link" :url="`/subpkg_archive/form/index?id=${item.id}`">
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patinetList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <view class="archive-tips">最多可添加6人</view>

      <!-- 下一步按钮 -->
      <button class="uni-button" v-if="type==='consult'" @click="nextStep">下一步</button>
    </view>
  </scroll-page>
</template>

<style lang="scss">
@import './index.scss';
</style>